package com.sandking.db.jedis;

import java.util.ArrayList;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import com.sandking.db.bean.Tzjl;
import java.util.List;
import java.util.Set;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;
import com.sandking.db.dao.TzjlDao;

public class TzjlJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Tzjl getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Tzjl tzjl = null;
		try{
			String key = "Tzjl_Object";
			String field = SK_Plus.b("id:",id).e();
			tzjl = Tzjl.createForJson(jedis.hget(key,field));
			if(tzjl == null){
				tzjl = TzjlDao.getById(id);
				if(tzjl!=null){
					loadCache(tzjl);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return tzjl;
	}
	
	/**
	 * 根据( tempid  挑战人id ) 查询
	 */
	public static Tzjl getByTempidTzrid(int tempid, int tzrid){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Tzjl tzjl = null;
		try{
			String key = "Tzjl_Index";
			String field = SK_Plus.b("tempid, tzrid:",tempid, tzrid).e();
			String primaryKey = jedis.hget(key,field);
			if(primaryKey!=null){
				tzjl = getById(Integer.valueOf(primaryKey));
			}
			if(tzjl == null){
				tzjl = TzjlDao.getByTempidTzrid(tempid, tzrid);
				if(tzjl!=null){
					loadCache(tzjl);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return tzjl;
	}
	
	/**
	 * 根据( 挑战人id ) 查询
	 */
	public static List<Tzjl> getByTzrid(int tzrid){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Tzjl> tzjls = new ArrayList<Tzjl>();
		try{
			String key = "Tzjl_Index";
			String field = SK_Plus.b(key,"tzrid:",tzrid).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Tzjl_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				tzjls = Tzjl.createForJson(jsons);
			}else{
				tzjls = TzjlDao.getByTzrid(tzrid);
				if(tzjls!=null && !tzjls.isEmpty()){
					loadCaches(tzjls);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return tzjls;
	}
	
	/**
	 * 根据( 目标人id ) 查询
	 */
	public static List<Tzjl> getByMbrid(int mbrid){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Tzjl> tzjls = new ArrayList<Tzjl>();
		try{
			String key = "Tzjl_Index";
			String field = SK_Plus.b(key,"mbrid:",mbrid).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Tzjl_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				tzjls = Tzjl.createForJson(jsons);
			}else{
				tzjls = TzjlDao.getByMbrid(mbrid);
				if(tzjls!=null && !tzjls.isEmpty()){
					loadCaches(tzjls);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return tzjls;
	}
	

	public static void loadCache(Tzjl tzjl){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(tzjl,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Tzjl tzjl,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Tzjl_Object";
		String field = SK_Plus.b("id:",tzjl.getId()).e();
		String data = tzjl.toJson();
		p.hset(key,field,data);
		
		// ---------------------- 唯一索引 ----------------------
		String primaryKey = String.valueOf(tzjl.getId());
		key = "Tzjl_Index";
		
		field = SK_Plus.b("tempid, tzrid:",tzjl.getTempid(),tzjl.getTzrid()).e();
		p.hset(key,field,primaryKey);
		
		// ---------------------- 聚集索引 ----------------------
		field = SK_Plus.b(key,"tzrid:",tzjl.getTzrid()).e();
		p.sadd(field, primaryKey);
		
		field = SK_Plus.b(key,"mbrid:",tzjl.getMbrid()).e();
		p.sadd(field, primaryKey);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Tzjl> tzjls){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Tzjl tzjl : tzjls){
				loadCache(tzjl,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Tzjl tzjl){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(tzjl,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Tzjl tzjl,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Tzjl_Object";
		String field = SK_Plus.b("id:",tzjl.getId()).e();
		p.hdel(key,field);
		
		// ---------------------- 唯一索引 ----------------------
		key = "Tzjl_Index";
		
		field = SK_Plus.b("tempid, tzrid:",tzjl.getTempid(),tzjl.getTzrid()).e();
		p.hdel(key,field);
		
		// ---------------------- 聚集索引 ----------------------
		String primaryKey = String.valueOf(tzjl.getId());
		field = SK_Plus.b(key,"tzrid:",tzjl.getTzrid()).e();
		p.srem(field, primaryKey);
		
		field = SK_Plus.b(key,"mbrid:",tzjl.getMbrid()).e();
		p.srem(field, primaryKey);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Tzjl> tzjls){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Tzjl tzjl : tzjls){
				clearCache(tzjl,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Tzjl insert(Tzjl tzjl){
		tzjl = TzjlDao.insert(tzjl);
		if(tzjl!=null){
			loadCache(tzjl);
		}
    	return tzjl;
    }
    
    public static Tzjl update(Tzjl tzjl){
    	tzjl = TzjlDao.update(tzjl);
    	if(tzjl!=null){
			loadCache(tzjl);
		}
    	return tzjl;
    }
    
    public static boolean delete(Tzjl tzjl){
    	boolean bool = TzjlDao.delete(tzjl);
    	if(bool){
    		clearCache(tzjl);
    	}
    	return bool;
    }
}